package com.studyweb.utils;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Paths;

public class ExcelTemplateGenerator {
    public static void main(String[] args) {
        String[] headers = {
            "题目内容",
            "题型",
            "难度",
            "选项A",
            "选项B",
            "选项C",
            "选项D",
            "正确答案",
            "解析"
        };

        try (Workbook workbook = new XSSFWorkbook()) {
            Sheet sheet = workbook.createSheet("题目导入模板");
            Row headerRow = sheet.createRow(0);

            // 创建单元格样式
            CellStyle headerStyle = workbook.createCellStyle();
            headerStyle.setAlignment(HorizontalAlignment.CENTER);
            Font headerFont = workbook.createFont();
            headerFont.setBold(true);
            headerStyle.setFont(headerFont);

            // 设置表头
            for (int i = 0; i < headers.length; i++) {
                Cell cell = headerRow.createCell(i);
                cell.setCellValue(headers[i]);
                cell.setCellStyle(headerStyle);
                sheet.setColumnWidth(i, 20 * 256); // 设置列宽
            }

            // 保存文件
            String templatePath = Paths.get("src", "main", "resources", "templates", "question_import_template.xlsx").toString();
            try (FileOutputStream fileOut = new FileOutputStream(templatePath)) {
                workbook.write(fileOut);
                System.out.println("Excel模板文件创建成功：" + templatePath);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}